<?php
//-------------------------------------------------------------------------
// OVIDENTIA http://www.ovidentia.org
// Ovidentia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//-------------------------------------------------------------------------
/**
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
 * @copyright Copyright (c) 2006 by CANTICO ({@link http://www.cantico.fr})
 */
include_once 'base.php';

require_once $GLOBALS['babInstallPath'] . '/addons/widgets/widgets/inputwidget.class.php';


/**
 * Constructs a Widget_Acl.
 *
 * @param string		$id			The item unique id.
 * @return Widget_LineEdit
 */
function Widget_Acl($id = null)
{
	return new Widget_Acl($id);
}



/**
 * A Widget_Acl is a widget that lets the user set access rights on a item.
 */
class Widget_Acl extends Widget_InputWidget implements Widget_Displayable_Interface 
{
	

	/**
	 * @param string $id			The item unique id.
	 * @return Widget_LineEdit
	 */
	public function __construct($id = null)
	{
		parent::__construct($id);
		
		// this uid will be used to verify the origin of ajax request
		$_SESSION['Widget_Acl_uid'] = uniqid('', true);
		$this->setMetaData('uid', $_SESSION['Widget_Acl_uid']);
		$this->setMetaData('selfpage', bab_getSelf());
		$this->setMetaData('buttonText', widget_translate('Set groups'));
		$this->setMetaData('withChildNodes', widget_translate('And the group children'));
		$this->setMetaData('setOfGroups', widget_translate('Set of groups'));
		$this->setMetaData('deleteTitle', widget_translate('Remove group'));
		
		$this->setMetaData('maxHeight', 150);
		$this->setMetaData('maxHeightUnit', 'px');
	}
	
	
	/**
	 * Sets the button text label.
	 *
	 * @param string $label
	 * @return Widget_Acl
	 */
	public function setLabel($label)
	{
		$this->setMetaData('buttonText', $label);

		return $this;
	}
	
	
	/**
	 * Set the maximum height of group list
	 * 
	 * @param	float 	$maxheight
	 * @param	string	$unit		em | px
	 * 
	 * @return Widget_Acl
	 */
	public function setMaxHeight($maxheight, $unit = 'em')
	{
		$this->setMetaData('maxHeight', $maxheight);
		$this->setMetaData('maxHeightUnit', $unit);
		
		return $this;
	}



	/**
	 * (non-PHPdoc)
	 * @see programs/widgets/Widget_InputWidget::getClasses()
	 */
	public function getClasses()
	{
		$classes = parent::getClasses();
		$classes[] = 'widget-acl';
		return $classes;
	}




	/**
	 * (non-PHPdoc)
	 * @see programs/widgets/Widget_Displayable_Interface::display()
	 */
	public function display(Widget_Canvas $canvas)
	{	
		
		if ($this->isDisplayMode()) {

			$classes = $this->getClasses();
			$classes[] = 'widget-displaymode';

			return $canvas->richtext(
					$this->getId(), 
					$classes, 
					$this->getValue(), 
					BAB_HTML_ALL ^ BAB_HTML_P,
					$this->getCanvasOptions()
			);


		} else {


			
			return $canvas->lineInput(
								$this->getId(),
								$this->getClasses(),
								$this->getFullName(),
								$this->getValue(),
								5,
								null,
								$this->isDisabled(),
								$this->getTitle()
							)
							.$canvas->metadata($this->getId(), $this->getMetadata());
		
		}
	}

}
